package com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.reconciliation;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Schema(description = "管理后台 - 财务管理 日汇总对账 Response VO")
@Data
@Builder
public class FinanceReconciliationSummaryVO {
    @Schema(description = "支付渠道")
    private String payChannel;
    @Schema(description = "对账日期")
    private String settlementDate;
    @Schema(description = "已支付总金额")
    private BigDecimal totalAmount;
    @Schema(description = "已支付总笔数")
    private int totalCount;
    @Schema(description = "银行到账总金额")
    private BigDecimal channelTotalAmount;
    @Schema(description = "通道费总金额")
    private int channelTotalCount;
    @Schema(description = "银行到账总笔数")
    private BigDecimal totalServiceCharge;
    @Schema(description = "对账结果")
    private Boolean success;

    public String getResult() {
        return (success != null && success) ? "√" : "×";
    }

    private List<FinanceReconciliationSummaryVO> channels;

    public static FinanceReconciliationSummaryVO defaultValue(String date) {
        return FinanceReconciliationSummaryVO.builder()
                .settlementDate(date)
                .totalAmount(BigDecimal.ZERO)
                .totalCount(0)
                .channelTotalAmount(BigDecimal.ZERO)
                .channelTotalCount(0)
                .totalServiceCharge(BigDecimal.ZERO)
                .success(true).build();
    }

    public FinanceReconciliationSummaryVO add(FinanceReconciliationSummaryVO b) {
        if (b == null) {
            return this;
        }

        if (channels == null) {
            channels = new ArrayList<>();
        }
        channels.add(b);
        this.totalAmount = add(this.totalAmount, b.getTotalAmount());
        totalCount += b.getTotalCount();
        this.channelTotalAmount = add(this.channelTotalAmount, b.getChannelTotalAmount());
        channelTotalCount += b.getChannelTotalCount();
        this.totalServiceCharge = add(totalServiceCharge, b.getTotalServiceCharge());
        this.success = b.getSuccess() ? this.success : false;
        return this;
    }

    private BigDecimal add(BigDecimal a, BigDecimal b) {
        return (a == null ? BigDecimal.ZERO : a).add((b == null ? BigDecimal.ZERO : b));
    }
}
